import { Popover } from 'antd';
import BaseListStore from './common/BaseListStore';

import * as API_LIBRARY from '../apis/libraryApi';
// import * as ODOC_LABEL from '../constants/label'

/**
 * 公文库页面 数据对象
 */
export default class OdocLibraryStore extends BaseListStore {
  constructor() {
    super();
    this.setTopTitle(this.getLabel2('383001', '公文库'));

    // this.API_CONDITION = "/api/odoc/odocDoclib/getCondition";
    // this.API_DATALIST = "/api/odoc/odocDoclib/odocLibrary";
    this.API_TABCOUNT = '';

    // this.searchStore.setConditionApi(this.API_CONDITION);
    // this.searchStore.setListApi(this.API_DATALIST);
  }

  getSearchsBaseValue() {
    return this.searchStore.form.getFormParams().odocSubject;
  }

  setSearchsBaseValue(v) {
    this.searchStore.form.updateFields({
      odocSubject: {
        value: v,
      },
    });
    this.searchStore.isSearched();
  }

  doSearch(params = {}) {
    // let param = this.searchStore.getFormParams();
    // Object.assign(param, param, this.searchStore.otherSearchParams);

    API_LIBRARY.getLibraryList(params).then((data) => {
      const sessionKey = data.sessionKey || data.generateParams.sessionkey;
      this.searchStore.tableStore.getDatas(sessionKey, params.current || 1);
    });
    this.searchStore.setShowSearchAd(false);
    return this;
  }

  buildSearchCondition(params = {}, defaultQueryCondition = {}) {
    API_LIBRARY.getCondition(params).then((data) => {
      this.searchStore.setSearchCondition(data);

      const conditioninfo = data.condition;
      this.searchStore.form.initFormFields(conditioninfo);

      this.searchStore.setKeywords(data.condition[0].searchHotTerm.dataList);

      this.searchStore.form.updateFields({
        odocSubject: {
          value: defaultQueryCondition.odocSubject,
        },
      });

      if (defaultQueryCondition.odoctype) {
        let odoctype = defaultQueryCondition.odoctype.split('|');
        this.searchStore.form.updateFields({
          odoctype: {
            value: [odoctype[0], [odoctype[1], odoctype[2]]],
            valueSpan: ['odocType'],
          },
        });
      }
      if (defaultQueryCondition.topictype) {
        let topictype = defaultQueryCondition.topictype.split('|');
        this.searchStore.form.updateFields({
          topictype: {
            value: [topictype[0], [topictype[1], topictype[2]]],
            valueSpan: ['odocTopicType'],
          },
        });
      }

      this.searchStore.form.updateFields({
        dispatchnum: {
          value: defaultQueryCondition.dispatchnum,
        },
      });

      if (defaultQueryCondition.writtendateselect) {
        let writtendateselect = defaultQueryCondition.writtendateselect.split('|');
        this.searchStore.form.updateFields({
          writtendateselect: {
            value: [writtendateselect[0], [writtendateselect[1], writtendateselect[2]]],
            valueSpan: ['odocTopicType'],
          },
        });
      }

      this.searchStore.form.updateFields({
        readstatus: {
          value: defaultQueryCondition.readstatus,
        },
      });
      this.searchStore.form.updateFields({
        perIntensive: {
          value: defaultQueryCondition.perIntensive,
        },
      });

      this.searchStore.isSearched();
      this.doSearch();
    });
  }

  resetForm() {
    this.searchStore.resetSearchForm();
    // this.searchStore.form.resetForm();
    this.setSearchsBaseValue('');
    // this.searchStore.isSearched();
  }


  getColumns = (columns) => {
    let newColumns = columns.map((column) => {
      if (column.dataIndex == 'id') {
        return {
          ...column,
          render: (text, record) => {
            let tableHtml = '  <table  style={{ width:400,height:120, border: 1 ,fontSize:12 }} cellSpacing="8" >';
            let popObjectArray = JSON.parse(record.odocinfospan);
            let colspanCount = 0;
            popObjectArray.forEach((popObject) => {
              if (popObject.showname == getLabel('24986', '标题')) {
                popObject.showname = '标&emsp;&emsp;题';
              }
              if (colspanCount == 0) {
                tableHtml += '<tr>';
              }
              tableHtml += `<th colspan='${popObject.shownamecolspan}'>${popObject.showname}：<i><i/></td>`;
              tableHtml += `<td width='350' colspan='${popObject.showvaluecolspan}'>${popObject.showvalue}</td>`;
              colspanCount += Number.parseInt(popObject.shownamecolspan) + Number.parseInt(popObject.showvaluecolspan);
              if (colspanCount >= 2) {
                tableHtml += '</tr>';
                colspanCount = 0;
              }
            });
            tableHtml += '</table>';
            return (<Popover ecId={`${this && this.props && this.props.ecId || ''}_Popover@z9eex8@popover`}
              overlayClassName="wea-odoc-library-popover"
              placement="bottomLeft"
              content={
                <div
                  dangerouslySetInnerHTML={{
                    __html: tableHtml,
                  }}
                />
              }
              trigger="hover"
            >
              <div dangerouslySetInnerHTML={{ __html: record.idspan }} />
            </Popover>);
          },
        };
      }
      // else if(column.dataIndex == "odoctype"){
      //   return {
      //     ...column, render: (text, record, index) => {
      //       return (<p  class="wea-odoctype-labelp" title={record.odoctypespan}>{record.odoctypespan}</p>);
      //     }
      //   }
      // }

      return column;
    });

    return newColumns;
  }
}
